package com.cehome.cloudbox.listener;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.cehome.cloudbox.constans.ExtensionTaskProperties;
import com.cehome.cloudbox.doman.dto.ProcCallBackParamDto;
import com.cehome.cloudbox.service.IProcCommonService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.Process;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.FlowableCancelledEvent;
import org.flowable.engine.history.HistoricProcessInstance;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 全局的流程取消监听
 * @author zfl
 */
@Component
@Slf4j
public class GlobalProcessCancelledListener extends AbstractFlowableEngineEventListener {

    @Resource
    protected RepositoryService repositoryService;
    @Resource
    private IProcCommonService iProcCommonService;
    @Resource
    private HistoryService historyService;

    @Override
    protected void processCancelled(FlowableCancelledEvent event) {
        log.info("全局的流程取消监听开始，流程实例ID：{}", event.getProcessInstanceId());

        // 流程实例
        HistoricProcessInstance hisProcIns = historyService.createHistoricProcessInstanceQuery()
                .includeProcessVariables()
                .processInstanceId(event.getProcessInstanceId())
                .singleResult();

        // 获取节点信息
        BpmnModel bpmnModel = repositoryService.getBpmnModel(hisProcIns.getProcessDefinitionId());
        Process process = bpmnModel.getProcesses().get(0);
    }
}
